<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-6-22
 * Time: 下午2:09
 */

namespace ActionsHttp;


use Data\Tree_customer_info;
use Data\Tree_customer_info_DAO;
use Data\Tree_verify_code_DAO;

class CustomerLoginByPhoneNumber extends BaseActionHttp
{
    public function doAction()
    {
        parent::doAction();
        // 登录方式
        $login_type = $this->data[LOGIN_TYPE];
        // 手机号
        $phone_number = (int)$this->data[PHONE_NUMBER];
        if (!preg_match("/^1[34578]\d{9}$/", $phone_number)) {
            return $this->SendErrMsg("非法手机号！");
        }

        switch ($login_type) {
            case PASSWORD_LOGIN:
                // 密码方式登录
                // 用户记录
                $customer_record = Tree_customer_info_DAO::getRecordByPhoneNumber($phone_number);
                if (!$customer_record) {
                    // 用户不存在
                    return $this->SendErrMsg("该手机号还未登录过,请使用手机号加验证码登录！");
                }
                // 用户存在
                // 数据库中用户密码
                $password_info = $customer_record->getPASSWORD();
                if (!$password_info) {
                    // 密码未设置过
                    return $this->SendErrMsg("用户未设置过密码，不能使用密码登录！");
                }
                // 密码已设置
                // 用户输入的登录密码
                $password = $this->data[PASSWORD];
                if ($password != $password_info) {
                    // 密码错误
                    return $this->SendErrMsg("手机号或密码错误！");
                }
                // 密码正确
                // 存储会话
                // 用户id
                $customer_id = $customer_record->getCUSTOMER_ID();
                $customer_record->setLOGIN_TIME(time());
                Tree_customer_info_DAO::update($customer_record);

                // 随机码
                $rand_str = $this->getRandomString(10);
                // 返回数据
                $this->setValue(CUSTOMER_ID,$rand_str.$customer_id);
                $this->ResponseDataToCurrentClient();
                break;

            case VERIFY_CODE_LOGIN:
                // 验证码登录
                // 获取验证码记录
                $verify_code_record = Tree_verify_code_DAO::getRecordByPhoneNumber($phone_number);
                if (!$verify_code_record) {
                    // 用户没有获取过验证码
                    return $this->SendErrMsg("没有获取过验证码！");
                }
                // 获取过验证码
                // 上次获取验证码的时间
                $verify_code_last_time = $verify_code_record->getVERIFY_CODE_TIME();
                if (time() - $verify_code_last_time > VERIFY_CODE_VALID_TIME) {
                    // 验证码已过期
                    return $this->SendErrMsg("验证码已过期！");
                }
                // 验证码在有效期内
                // 数据库中验证码
                $verify_code_info = $verify_code_record->getVERIFY_CODE();
                // 用户输入的验证码
                $verify_code = $this->data[VERIFY_CODE];
                if ($verify_code != $verify_code_info) {
                    // 验证码错误
                    return $this->SendErrMsg("验证码错误！");
                }
                // 验证码正确
                // 用户记录
                $customer_record = Tree_customer_info_DAO::getRecordByPhoneNumber($phone_number);
                if (!$customer_record) {
                    // 新用户
                    // 用户注册
                    $customer_record = new Tree_customer_info(array());
                    $customer_record->setPHONE_NUMBER($phone_number);
                    $customer_record->setLOGIN_TIME(time());
                    // 插入数据库
                    $customer_id = Tree_customer_info_DAO::insert($customer_record);
                    // 随机码
                    $rand_str = $this->getRandomString(10);
                    // 返回数据
                    $this->setValue(CUSTOMER_ID,$rand_str.$customer_id);
                    $this->ResponseDataToCurrentClient();
                } else {
                    // 老用户
                    // 用户id
                    $customer_id = $customer_record->getCUSTOMER_ID();
                    $customer_record->setLOGIN_TIME(time());
                    Tree_customer_info_DAO::update($customer_record);

                    // 随机码
                    $rand_str = $this->getRandomString(10);
                    // 返回数据
                    $this->setValue(CUSTOMER_ID,$rand_str.$customer_id);
                    $this->ResponseDataToCurrentClient();
                }

                break;

            default:
                return $this->SendErrMsg("没有该登录类型！");
                break;
        }
    }

    function getRandomString($len, $chars=null)
    {
        if (is_null($chars)){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

}